/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-14 22:08:03
 * @LastEditTime: 2022-08-20 23:21:31
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




extern void H_NES_Mapper74_CHR_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper74_CHR_Read(Huint16 Address,void* v);




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper4




static void DeInit(void* v){
  H_NES_Console* Console;
  void (*DeInit)(void*);

  Console=v;

  DeInit=Console->ThisMapper.Variant.Mapper74.DeInit;
  if(DeInit!=NULL){
    DeInit(v);
  }
  Console->Memory.Free(Console->ThisMapper.Variant.Mapper74.CHR_RAM);
}

static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  Console->ThisMapper.Variant.Mapper74.Get(Console,Progress);

  p=&p[Console->ThisMapper.Variant.Mapper74.ProgressSize];

  for(i=0;i<0x800;i++){
    p[i]=Console->ThisMapper.Variant.Mapper74.CHR_RAM[i];
  }

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  p=&p[Console->ThisMapper.Variant.Mapper74.ProgressSize];

  for(i=0;i<0x800;i++){
    Console->ThisMapper.Variant.Mapper74.CHR_RAM[i]=p[i];
  }

  Console->ThisMapper.Variant.Mapper74.Load(Console,Progress);

}

int H_NES_Mapper74_Init(void* v){
  H_NES_Console* Console;
  int r;

  extern int H_NES_Mapper4_Init(void* v);
  
  
  Console=v;

  Console->ThisMapper.Variant.Mapper74.CHR_RAM=Console->Memory.Malloc(0x800);
  if(Console->ThisMapper.Variant.Mapper74.CHR_RAM==NULL){
    return vH_NES_Result_CHR_RAM_AllocFail;
  }

  r=H_NES_Mapper4_Init(v);
  if(r!=vH_NES_Result_OK){
    Console->Memory.Free(Console->ThisMapper.Variant.Mapper74.CHR_RAM);
    return r;
  }

  Console->Cartridge.CHR_Write=H_NES_Mapper74_CHR_Write;
  Console->Cartridge.CHR_Read=H_NES_Mapper74_CHR_Read;

  Console->ThisMapper.Variant.Mapper74.DeInit=Console->Cartridge.Mapper.DeInit;
  Console->ThisMapper.Variant.Mapper74.ProgressSize=Console->Cartridge.Progress.Size;
  Console->ThisMapper.Variant.Mapper74.Get=Console->Cartridge.Progress.Get;
  Console->ThisMapper.Variant.Mapper74.Load=Console->Cartridge.Progress.Load;
  Console->Cartridge.Mapper.DeInit=DeInit;
  Console->Cartridge.Progress.Size+=0x800;
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;


  return vH_NES_Result_OK;
}




#undef ThisMapper



